//
//  LCYIMMessage.m
//  IMByLinkUs
//
//  Created by lcy on 16/1/19.
//  Copyright © 2016年 lcy. All rights reserved.
//

#import "LCYIMMessage.h"
#import "NSDate+LCYCurrentDate.h"

@implementation LCYIMMessage

#pragma mark - Initialization

// 默认
+ (instancetype)messageWithSenderId:(NSString *)senderId
                        displayName:(NSString *)displayName
                               text:(NSString *)text
{
    return [[self alloc] initWithSenderId:senderId
                        senderDisplayName:displayName
                                     date:[NSDate getCurrentDate]
                                     text:text];
}

- (instancetype)initWithSenderId:(NSString *)senderId
               senderDisplayName:(NSString *)senderDisplayName
                            date:(NSDate *)date
                            text:(NSString *)text
{
    NSParameterAssert(text != nil);
    
    self = [self initWithSenderId:senderId senderDisplayName:senderDisplayName date:date];
    if (self) {
        _text = [text copy];
    }
    return self;
}


- (instancetype)initWithSenderId:(NSString *)senderId
               senderDisplayName:(NSString *)senderDisplayName
                            date:(NSDate *)date
{
    NSParameterAssert(senderId != nil);
    NSParameterAssert(senderDisplayName != nil);
    NSParameterAssert(date != nil);
    
    self = [super init];
    if (self) {
        _senderId = [senderId copy];
        _senderDisplayName = [senderDisplayName copy];
        _date = [date copy];
    }
    return self;
}






// 语音
+ (instancetype)messageWithSenderId:(NSString *)senderId
                        displayName:(NSString *)displayName
                               date:(NSDate *)date
                               text:(NSString *)text
                      audioFileName:(NSString *)audioFileName
{
    return [[self alloc] initWithSenderId:senderId
                        senderDisplayName:displayName
                                     date:[NSDate getCurrentDate]
                                     text:text
                            audioFileName:audioFileName];
}


- (instancetype)initWithSenderId:(NSString *)senderId
               senderDisplayName:(NSString *)senderDisplayName
                            date:(NSDate *)date
                            text:(NSString *)text
                   audioFileName:(NSString *)audioFileName
{
    self = [self initWithSenderId:senderId senderDisplayName:senderDisplayName date:date];
    if (self) {
        _audioFileName = [audioFileName copy];
        _text = [text copy];
    }
    return self;
}







// 附件

+ (instancetype)messageWithSenderId:(NSString *)senderId
                        displayName:(NSString *)displayName
                               date:(NSDate *)date
                               text:(NSString *)text
                            fileURL:(NSURL *)fileURL
                           fileData:(NSData *)fileData
{
    return [[self alloc] initWithSenderId:senderId
                        senderDisplayName:displayName
                                     date:[NSDate getCurrentDate]
                                     text:text
                                  fileURL:fileURL
                                 fileData:fileData];
}


- (instancetype)initWithSenderId:(NSString *)senderId
               senderDisplayName:(NSString *)senderDisplayName
                            date:(NSDate *)date
                            text:(NSString *)text
                         fileURL:(NSURL *)fileURL
                        fileData:(NSData *)fileData
{
    self = [self initWithSenderId:senderId senderDisplayName:senderDisplayName date:date];
    if (self) {
        _fileData = [fileData copy];
        _fileURL = [fileURL copy];
    }
    return self;
}

- (id)init
{
    NSAssert(NO, @"%s is not a valid initializer for %@.", __PRETTY_FUNCTION__, [self class]);
    return nil;
}

- (void)dealloc
{
    _senderId = nil;
    _senderDisplayName = nil;
    _date = nil;
    _text = nil;
}

- (NSUInteger)messageHash
{
    return self.hash;
}


#pragma mark - NSObject

- (BOOL)isEqual:(id)object
{
    if (self == object) {
        return YES;
    }
    
    if (![object isKindOfClass:[self class]]) {
        return NO;
    }
    
    LCYIMMessage *aMessage = (LCYIMMessage *)object;
    
    
    return [self.senderId isEqualToString:aMessage.senderId]
    && [self.senderDisplayName isEqualToString:aMessage.senderDisplayName]
    && ([self.date compare:aMessage.date] == NSOrderedSame);
}

- (NSUInteger)hash
{
    NSUInteger contentHash = self.text.hash;
    return self.senderId.hash ^ contentHash;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"<%@: senderId=%@, senderDisplayName=%@, date=%@, text=%@>",
            [self class], self.senderId, self.senderDisplayName, self.date, self.text];
}

//- (id)debugQuickLookObject
//{
//    return [self.media mediaView] ?: [self.media mediaPlaceholderView];
//}

#pragma mark - NSCoding

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        _senderId = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(senderId))];
        _senderDisplayName = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(senderDisplayName))];
        _date = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(date))];
        _text = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(text))];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.senderId forKey:NSStringFromSelector(@selector(senderId))];
    [aCoder encodeObject:self.senderDisplayName forKey:NSStringFromSelector(@selector(senderDisplayName))];
    [aCoder encodeObject:self.date forKey:NSStringFromSelector(@selector(date))];
    [aCoder encodeObject:self.text forKey:NSStringFromSelector(@selector(text))];
    
}

#pragma mark - NSCopying

- (instancetype)copyWithZone:(NSZone *)zone
{
    if(self.audioFileName.length != 0)
    {
        return [[[self class] allocWithZone:zone] initWithSenderId:self.senderId
                                                 senderDisplayName:self.senderDisplayName
                                                              date:self.date
                                                              text:self.text
                                                     audioFileName:self.audioFileName];
    }
    
    if(self.fileData != nil)
    {
        return [[[self class] allocWithZone:zone] initWithSenderId:self.senderId
                                                 senderDisplayName:self.senderDisplayName
                                                              date:self.date
                                                              text:self.text
                                                           fileURL:self.fileURL
                                                          fileData:self.fileData];
    }
    
    return [[[self class] allocWithZone:zone] initWithSenderId:self.senderId
                                             senderDisplayName:self.senderDisplayName
                                                          date:self.date
                                                          text:self.text];
}

@end
